#coding=utf-8
import os
import wx
import pickle

class ConfigDialog(wx.Dialog):
    def __init__(self, parent):
        wx.Dialog.__init__(self, parent, -1,size=(400,300))
        
        panel = wx.Panel(self) 
        
        sizer0 = wx.BoxSizer(wx.VERTICAL)
        
        sizer = wx.GridBagSizer(hgap=5, vgap=5)
        
        box = wx.BoxSizer(wx.VERTICAL)
        label = wx.StaticText(panel, -1, unicode("选择搜索引擎:", 'u8'))
        box.Add(label, proportion=0, flag=wx.ALL, border=8)
        self.lb = wx.CheckListBox(panel, -1, style=wx.LB_MULTIPLE, choices=options.search_engines)
        for se in options.search_engines_chosed:
            n = self.lb.FindString(se)
            self.lb.Check(n, True)
        box.Add(self.lb, proportion=1, flag=wx.ALL)
        sizer.Add(box, pos=(0,0), span=(6,1))
        
        box = wx.BoxSizer(wx.HORIZONTAL)
        label = wx.StaticText(panel, -1, unicode("设置线程数量:", 'u8'))
        box.Add(label, flag=wx.ALL, border=8)
        self.spin = wx.SpinCtrl(panel, -1)
        self.spin.SetRange(1,10)
        self.spin.SetValue(options.thread_num)
        box.Add(self.spin, flag=wx.ALL, border=4)
        sizer.Add(box, pos=(1,1))
        
        box = wx.BoxSizer(wx.VERTICAL)
        label = wx.StaticText(panel, -1, unicode("设置数据文件目录:", 'u8'))
        box.Add(label, flag=wx.ALL, border=8)
        
        box1 = wx.BoxSizer(wx.HORIZONTAL)
        self.tc = wx.TextCtrl(panel, -1, value=options.datadir, 
                              size=(150,-1), style=wx.TE_READONLY)
        box1.Add(self.tc, flag=wx.ALL, border=8)
        bt = wx.Button(panel,-1, label=unicode("选择", 'u8'))
        self.Bind(wx.EVT_BUTTON, self.OnClick, bt)
        box1.Add(bt, flag=wx.ALL, border=8)
        
        box.Add(box1)
        sizer.Add(box, pos=(3,1))
        
        sizer0.Add(sizer)
        
        line = wx.StaticLine(panel, -1, size=(20,-1), style=wx.LI_HORIZONTAL)
        sizer0.Add(line, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.RIGHT|wx.TOP, 5)
        
        btnsizer = wx.StdDialogButtonSizer()
        btn = wx.Button(panel, wx.ID_OK)
        btn.SetDefault()
        btnsizer.AddButton(btn)
        
        btn = wx.Button(panel, wx.ID_CANCEL)
        btnsizer.AddButton(btn)
        
        btnsizer.Realize()
        
        sizer0.Add(btnsizer, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
        
        panel.SetSizer(sizer0)
        sizer0.Fit(panel)
        
    def OnClick(self, event):
        dlg = wx.wx.DirDialog(self, style=0,
                message=unicode("请选择数据文件目录", 'u8')
                )
                
        if dlg.ShowModal() == wx.ID_OK:
            self.tc.SetValue(dlg.GetPath())
    
    def update_options(self):
        global options
        options.search_engines_chosed = [self.lb.GetString(i) for i in range(self.lb.GetCount()) 
                                    if self.lb.IsChecked(i)]
        options.thread_num = self.spin.GetValue()
        options.datadir = self.tc.GetValue()
        
        save_options(options)
        
class TestFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'TestFrame', 
                size=(400, 300))
        
        panel = wx.Panel(self, -1)
        self.bt = wx.Button(panel, -1, "show")
        self.Bind(wx.EVT_BUTTON, self.OnClick, self.bt)
    
    def OnClick(self, event):
        cd = ConfigDialog(self)
        if cd.ShowModal() == wx.ID_OK:
            cd.update_options()
            print options.search_engines
            print options.search_engines_chosed
            print options.thread_num, type(options.thread_num)
            print options.datadir 
            
class Options(object):
    def __init__(self):
        self.search_engines = None
        self.search_engines_chosed = None
        self.thread_num = None
        self.datadir =None
        
def get_or_create_options():
    if not os.path.exists(options_fn):
        o = Options()
        
        o.search_engines = ['google_com', 'google_cn', 'yahoo_com', 'yahoo_cn', 'ask', 'baidu']
        o.search_engines_chosed = ['google_cn', 'ask']
        
        o.thread_num = 5
        o.datadir = '/home/jay/sg'
    
        f = open(options_fn, 'wb')
        pickle.dump(o, f)
        
        return o
    else:
        return pickle.load(open(options_fn))

def save_options(o):
    f = open(options_fn, 'wb')
    pickle.dump(o, f) 
    
options_fn = './options.pickle'
options = get_or_create_options()
    
if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = TestFrame()
    frame.Show()
    app.MainLoop()
        
